{ Класс таблица с заголовком и данными

  Module is a part of project http://code.google.com/p/drivers-training-request
  Copyright (C) 2012 Leonid Vlasov leoniv@bk.ru

  This source is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  A copy of the GNU General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.
}

TGridTemplate = Class;
{ TColumnList}

{ TColumnsList }

TColumnsList = Class(TList)

public
      function Add(Item: Pointer): Integer;
      function Index(Name:String): Integer;
      function Name(AIndex:Integer):string;
end;

TDataTable = Class;
{ TDataTableRow}

TDataTableRow = Class
  FDataTAble:TDataTable;
  FRowIndex:Integer;
private
  FIndex: String;
  function GetFieldCOunt: Integer;
  function GetValueByName(FieldName:String):String;
  function GetValueByIndex(Index: Integer): String;
  procedure SetValueByIndex(ColIndex: Integer; AValue: String);
  procedure SetValueByName(FieldName: String; AValue: String);
  function GetContext(Const FormatStr,ToFrom,ColName,Message:String):String;
  public
  constructor Create(ADataTAble:TDataTable;ARowIndex:Integer);
  Procedure Delete;
  //Валидация значения
  function Validate(Var MessArrat:TMessagesArray):Boolean;
  Function LogicalValidate(Cause:String):Boolean;
  //Конвертация
  function ToXMLString():String;
  function ToXMLNode(Const XMLDoc:TXMLDocument):TDOMElement;
  function ToSQLString():String;
  procedure FromXMLNode(Node:TDOMElement);
  procedure FromSQLFildVAlue(Const SQLVAlue:TDataSet);
  //
  Property RowIndex:String read FIndex;
  Property FieldCount:Integer read GetFieldCOunt;
  Property ValueByName[FieldName:String]:String read GetValueByName write SetValueByName;
  Property ValueByIndex[ColIndex:Integer]:String read GetValueByIndex write SetValueByIndex;
end;

{ TDataTable }

TDataTable = Class(TStrDynArray)
  FColumns:TColumnsList;
  FGrid:TStringGrid;
//  FName:
//Если грид имел бработчики перед тем как мы его приделали к аррею тогда сохраняем их здесь
  FOnGridColRowMovedOld:TgridOperationEvent;
  FOnGridEditingDoneOld:TNotifyEvent;
  FOnGridGetEditMaskOld:TGetEditEvent;
  FOnGridGetEditTextOld:TGetEditEvent;
  FOnGridKeyDown:TKeyEvent;
  procedure SetLength(Cols,Rows: Integer);Override;
  procedure AddCol();Override;
  procedure InsertCol(Index:Integer);Override;
  procedure DeleteColRow(IsColumn: Boolean; Index: Integer);Override;
 private
   function GetFD(AIndex: Integer): TFieldDescriptor;
   function GetFD(ColName: String): TFieldDescriptor;
    function GetGrid(GridPattern: TGridTemplate): TStringGrid;
    function GetRow(AIndex: Integer): TDataTableRow;
    function GetString(Col, Row: Integer): String;
    procedure SetString(Col, Row: Integer;AValue:String);
  //TStrDynArray events
  procedure OnArrSetValue(Sender: TObject; ACol,ARow: integer; ItemVAlue:String);
  procedure OnArrAddRow(Sender:TObject;Index:Integer);
  procedure OnArrAddCol(Sender:TObject;Index:Integer;ColDesc:Pointer);
  procedure OnArrDelRow(Sender:TObject;Index:Integer);
  procedure OnArrDelCol(Sender:TObject;Index:Integer);
  procedure OnArrColRowMoved(Sender: TObject; IsColumn: Boolean;sIndex, tIndex: Integer);
  //TStringGrid events
  procedure OnGridColRowMoved(Sender: TObject; IsColumn: Boolean; sIndex, tIndex: Integer);
  procedure OnGridEditingDone(Sender: TObject);
  procedure OnGridGetEditMask(Sender: TObject; ACol, ARow: Integer; var Value: string);
  procedure OnGridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
  procedure OnGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  //
  function GetColCount: Integer;
  procedure SetGrid(GridPattern:TGridTemplate; AValue: TStringGrid);
 //  procedure SetOnSetValue(AValue: TOnNotifyStringItem);
public
 Constructor Create;
 property ColCount:Integer read GetColCount;
 procedure AddCol(Fdesc:TFieldDescriptor);
 function  AddRow():TDataTableRow;
 property Grid[GridPattern:TGridTemplate]:TStringGrid read GetGrid write SetGrid;
 procedure DelCol(Index:Integer);Override;
 procedure Clear;Override;
 procedure GetEditMask(Col:integer; var Value: String);
 procedure InsertCol(Fdesc:TFieldDescriptor;Index:Integer);
 function ColIndex(AColName:String): Integer;
 function ColName(AColIndex:Integer):string;
 property Rows[Index:Integer]:TDataTableRow Read GetRow;
 property FieldDescriptorByName[AColName:String]:TFieldDescriptor read GetFD;
 property FieldDescriptorByIndex[AIndex:Integer]:TFieldDescriptor read GetFD;
 property Cells[Col,Row:Integer]:String read GetString write SetString;

 // property OnSetValue:TOnNotifyStringItem read FOnSetValue write SetOnSetValue;
 //property OnAddRow:TOnNotifyColRow read FOnAddRow;
 //property OnAddCol:TOnNotifyAddCol read FOnAddCol;
 //property OnDelRow:TOnNotifyColRow read FOnDelRow;
 //property OnDelCol:TOnNotifyColRow read FOnDelCol;
 //property OnMoveColRow:TOperationEvent read FOnMoveColRow;
end;

